﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HPIT.RentHouse.IService;
using HPIT.RentHouse.Service;
using HPIT.RentHouse.Admin.Models;
using HPIT.RentHouse.DTO;

namespace HPIT.RentHouse.Admin.Controllers
{
    [Authorize]
    public class PermissionsController : Controller
    {
        //反转注入
        private IServicePermissions _permissionsService;
        public PermissionsController(IServicePermissions permissionsService)
        {
            _permissionsService = permissionsService;
        }
        /// <summary>
        /// 权限列表(视图)
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {           
            return View();
        }
        /// <summary>
        /// 权限列表显示(方法)
        /// </summary>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult GetList(int start,int length,string name)
        {                               
            int count = 0;
            var list = _permissionsService.GetPageList(start, length, name, ref count);
            PageModel pageModel = new PageModel();  
            pageModel.data = list;  
            pageModel.recordsTotal= count;  
            pageModel.recordsFiltered= count;   
            return Json(pageModel); 
        }      
        /// <summary>
        /// 添加权限页面(视图)
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            return View();
        }
        /// <summary>
        /// 添加权限(方法)
        /// </summary>
        /// <param name="dTO"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddPower(PerissionDTO dTO)
        {
            var result = _permissionsService.Add(dTO);
            return Json(result);
        }
        /// <summary>
        /// 根据id查询出该条数据并传递给视图(方法+视图)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(long id)
        {
            PerissionDTO dto = _permissionsService.Get(id);
            return View(dto);
        }
        /// <summary>
        /// 修改权限(方法)
        /// </summary>
        /// <param name="dTO"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditPer(PerissionDTO dTO)
        { 
           var res=_permissionsService.Update(dTO);
            return Json(res);
        }
        /// <summary>
        /// 删除权限(方法)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(long id)
        { 
            var res=_permissionsService.Delete(id);
            return Json(res);
        }
        /// <summary>
        /// 批量删除权限(方法)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteBatch(List<long> ids)
        {
            var res = _permissionsService.DeleteBatch(ids);
            return Json(res);
        }
    }
}